package org.hgvc.webapp.ets.jsf.beans.finance;

import java.util.List;

import org.hgvc.webapp.ets.bo.ActionBulletin;
import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.bo.Invoice;
import org.hgvc.webapp.ets.bo.InvoiceDtl;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.MemberComp;
import org.hgvc.webapp.ets.bo.MemberInd;
import org.hgvc.webapp.ets.bo.Payment;
import org.hgvc.webapp.ets.bo.PaymentDtl;
import org.hgvc.webapp.ets.bo.StandingInstruction;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.jsf.beans.ActionBulletinBean;
import org.hgvc.webapp.ets.spring.service.FinanceService;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class MemberPaymentBean extends AbstractBean{
	public static final String BEAN_NAME="memberPaymentBean";
	public static final String NAV_PORTAL="financePortal";
	private FinanceService service;
	private Payment payment;
	private PaymentDtl paymentDtl;
	private List<InvoiceDtl> list;
	private Member member;
	private String reason;
	private String password;
	public static final String NAV_VIEW_MEMBER_PAYMENT="financeConfirmMemberPayment";
	private String actionNo;
	private StandingInstruction instruction;
	private Agreement agreement;
	
	private MemberInd memberInd;
	private MemberComp memberComp;
	
	private MemberService memberService;
	
	
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public MemberInd getMemberInd() {
		return memberInd;
	}
	public void setMemberInd(MemberInd memberInd) {
		this.memberInd = memberInd;
	}
	public MemberComp getMemberComp() {
		return memberComp;
	}
	public void setMemberComp(MemberComp memberComp) {
		this.memberComp = memberComp;
	}
	public FinanceService getService() {
		return service;
	}
	public void setService(FinanceService service) {
		this.service = service;
	}
	public Payment getPayment() {
		return payment;
	}
	public void setPayment(Payment payment) {
		this.payment = payment;
	}
	public PaymentDtl getPaymentDtl() {
		return paymentDtl;
	}
	public void setPaymentDtl(PaymentDtl paymentDtl) {
		this.paymentDtl = paymentDtl;
	}
	public List<InvoiceDtl> getList() {
		return list;
	}
	public void setList(List<InvoiceDtl> list) {
		this.list = list;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public String getReason() {
		return reason;
	}
	public void setReason(String reason) {
		this.reason = reason;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getActionNo() {
		return actionNo;
	}
	public void setActionNo(String actionNo) {
		this.actionNo = actionNo;
	}
	public StandingInstruction getInstruction() {
		return instruction;
	}
	public void setInstruction(StandingInstruction instruction) {
		this.instruction = instruction;
	}
	public Agreement getAgreement() {
		return agreement;
	}
	public void setAgreement(Agreement agreement) {
		this.agreement = agreement;
	}
	boolean addNewAgreementForExistMember=false;
	//-----------------------------------------------------------------------------------------------------------
	//action bulletin call method
	public String viewMemberPayment(String actionNo,String... params){
		String paymentId = params[1];
		 
		if (params.length>2 && params[2]!=null && params[2].equalsIgnoreCase("newagreement")){
			addNewAgreementForExistMember = true;
		}else{
			addNewAgreementForExistMember = false;
		}
		this.actionNo = actionNo;
		try{
			if(paymentId!=null){
				Object[] array = service.getMemberPaymentInfoByPaymentId(paymentId);
				payment = (Payment)array[0];
				paymentDtl = (PaymentDtl)array[1] ;
				list= (List<InvoiceDtl>)array[2];
				member= (Member)array[3];
				instruction = (StandingInstruction)array[4];
				agreement = (Agreement)array[5];
				
				
				if (member.getMemberType().equalsIgnoreCase(CommonConstant.MEMBER_TYPE_IND)){
					
					this.memberInd = this.memberService.getMemberIndByMemberId(member.getMemberId());
					
				}else{
					
					this.memberComp = this.memberService.getMemberCompByMemberId(member.getMemberId());
				}
			}
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_VIEW_MEMBER_PAYMENT;
	}

	public String confirmMemberDepositPayment(){
		try{
			if(list.size()==1){
				InvoiceDtl dtl = list.get(0);
				if(dtl.getItemType().equalsIgnoreCase(CommonConstant.INVOICE_TYPE_DEPOSIT)){
					service.memberDepositPaymentClearance(member,agreement, payment, getThisUser(), password,reason,actionNo,list,addNewAgreementForExistMember);
				}else{
					service.memberPaymentClearance(member, agreement, payment, getThisUser(), password, reason, actionNo,list);
				}
			}else{
				service.memberPaymentClearance(member, agreement, payment, getThisUser(), password, reason, actionNo,list);
			}
			
			return NAV_PORTAL;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return null;
	}

	public String rejectMemberPayment(){
		/*try{
			//service.memberDepositPaymentRejection(member, payment, getThisUser(), password, reason);
			return NAV_PORTAL;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}*/

		return null;
	}

}
